﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QAHelper.Common;
using System.Reflection;

namespace WatiNTest
{
    public static class TestManager
    {
        public static TestScenario testScenario = null;
        public static string traceLogFileName = string.Empty;
        private static string thisDateTime = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString();
        public static string EmailToLogin = string.Empty;
        public static string PasswordToLogin = string.Empty;
        static string browser_type = string.Empty;
        static string url_to_test = string.Empty;
        public static WatiN.Core.Browser browser = null;

        public static string BROWSER_TYPE
        {
            set { browser_type = value; }
            get { return browser_type; }
        }

        public static string URL_TO_TEST
        {
            set { url_to_test = value; }
            get { return url_to_test; }
        }
       

        
        public static void InvokeTestMethod(string testMethodName, Type typeOfClass, object testObj, TestScenario scenario)
        {
            try
            {
                testScenario = scenario;
                /*string[] tempAuth = CommonHelper.FetchAuthDataFromRawTestData(scenario.TestData, "[Auth]");

                if (tempAuth != null)
                {
                    EmailToLogin = tempAuth[0];
                    PasswordToLogin = tempAuth[1];
                }*/
                typeOfClass.InvokeMember("InitTest", BindingFlags.Default | BindingFlags.InvokeMethod, null, testObj, null);
                typeOfClass.InvokeMember(testMethodName, BindingFlags.Default | BindingFlags.InvokeMethod, null, testObj, null);                
            }
            catch (Exception exp)
            {
                Console.WriteLine("\t\t{0} Uexpected error occurred in {1}. Message is: {2}", thisDateTime, scenario.TestCaseID, exp.Message);
                System.IO.File.AppendAllText(traceLogFileName, string.Format("{0} Uexpected error occurred in {1}. Message is: {2}"+Environment.NewLine, thisDateTime, scenario.TestCaseID, exp.InnerException.Message));
            }
            finally
            {
                typeOfClass.InvokeMember("TeardownTest", BindingFlags.Default | BindingFlags.InvokeMethod, null, testObj, null);
            }
        }
    }
}
